package wx

import (
	"errors"

	"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
	"github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram"

	"gin-api-server/core_library/global"
)

func InitWxMiniapp() error {
	wechatConf := global.Config.Wechat.MiniApp
	if wechatConf.Appid == "" {
		return nil
	}
	redisConf := global.Config.Redis
	program, err := miniProgram.NewMiniProgram(&miniProgram.UserConfig{
		AESKey:    wechatConf.Secret,
		AppID:     wechatConf.Appid,
		Secret:    wechatConf.Secret,
		HttpDebug: false,
		Debug:     false,
		Log: miniProgram.Log{
			Level: "debug",
			File:  global.RootPath + "/runtime/logs/wechat-miniapp.log",
		},
		Cache: kernel.NewRedisClient(&kernel.RedisOptions{
			Addr:     redisConf.Host + ":" + redisConf.Port,
			Password: redisConf.Password,
			DB:       redisConf.Db,
		}),
	})
	if err != nil {
		return errors.New("初始化 wechat miniapp 失败:" + err.Error())
	}
	global.WechatMiniapp = program
	return nil
}
